﻿using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Windows.Media.Imaging;

namespace ImageLib
{
    public class ImageItem : INotifyPropertyChanged, ICloneable
    {
        public int MODE = 0;
        private BitmapImage _img = null;
        public BitmapImage Img
        {
            get { return _img; }
            set
            {
                if (MODE == 0) // this is an temporary object
                    return;
                try // this is the object the UI will use (a clone of the temp object created by the thread)
                {
                    _img = new BitmapImage();
                    _img.DownloadCompleted += new EventHandler(DownloadDataCompleted);
                    _img.BeginInit();
                    _img.UriCachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                    _img.CacheOption = BitmapCacheOption.None;
                    _img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                    _img.UriSource = new Uri(_imgurl);
                    _img.EndInit();
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }
        }

        private void DownloadDataCompleted(object sender, EventArgs args)
        {
            try
            {
                Console.WriteLine("Image Downloaded : " + _name);
                RaisePropertyChanged("Img");
                if (_img.Width <= 175 && _img.Height <= 175)
                    Mode = "None";
                else
                    Mode = "Uniform";
                Resolution = _img.Width + "x" + _img.Height;
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
        }

        private String _resolution = String.Empty;
        public String Resolution
        {
            get { return _resolution; }
            set
            {
                _resolution = value;
                RaisePropertyChanged("Resolution");
            }
        }

        private String _mode = "None";
        public String Mode
        {
            get { return _mode; }
            set
            {
                _mode = value;
                RaisePropertyChanged("Mode");
            }
        }

        private String _imgurl = String.Empty;
        public String ImgUrl
        {
            get { return _imgurl; }
            set
            {
                _imgurl = value;
                Img = null;
                RaisePropertyChanged("ImgUrl");
            }
        }

        private String _name = String.Empty;
        public String Name
        {
            get { return _name; }
            set
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }

        private String _link = String.Empty;
        public String Link
        {
            get { return _link; }
            set
            {
                _link = value;
                RaisePropertyChanged("Link");
            }
        }

        private Boolean _checked = true;
        public Boolean Checked
        {
            get { return _checked; }
            set 
            {
                _checked = value;
                RaisePropertyChanged("Checked");
            }
        }

        private Boolean _selected = false;
        public Boolean Selected
        {
            get { return _selected; }
            set
            {
                _selected = value;
                RaisePropertyChanged("Selected");
            }
        }

        private long _max = 145;
        public long Max
        {
            get { return _max; }
            set
            {
                _max = value;
                RaisePropertyChanged("Max");
            }
        }

        private long _advance = 0;
        public long Advance
        {
            get { return _advance; }
            set
            {
                _advance = value;
                RaisePropertyChanged("Advance");
            }
        }

        private string _path;
        public void Save(String path)
        {
            _path = path;
            if (_img != null && path != String.Empty)
            {
                try
                {
                    WebClient wc = new WebClient();
                    wc.DownloadDataCompleted += new DownloadDataCompletedEventHandler(SaveDataCompleted);
                    wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(SaveProgressChanged);
                    wc.DownloadDataAsync(new Uri(_imgurl));
                }
                catch (Exception ex) { Console.WriteLine(ex.Message); }
            }
        }

        public void SaveDataCompleted(object sender, DownloadDataCompletedEventArgs args)
        {
            try
            {
                if (!Directory.Exists(_path))
                {
                    Directory.CreateDirectory(_path);
                }
                FileStream fs = new FileStream(_path + "//" + _name, FileMode.Create);
                fs.Write(args.Result, 0, args.Result.Length);
                fs.Close();
                Advance = 145;
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
        }

        public void SaveProgressChanged(object sender, DownloadProgressChangedEventArgs args)
        {
            Advance = args.ProgressPercentage;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        #endregion

        public object Clone()
        {
            ImageItem item = new ImageItem() {MODE = 1, ImgUrl = _imgurl, Name = _name };
            return item;
        }
    }
}